var supplierObj = [];
var limit = 30;
var tid;
var orgObj = convertOrgToObj(orgList);
var inventoryTypeObj = convertEnumToObj(inventoryTypeList);
var storageObj = convertEnumToObj(storageList);
var materialObj = convertEnumToObj(materialList);
var controlStorageObj = convertEnumToObj(controlStorageList);
// var breedTypeObj = convertEnumToObj(breedTypeList);
// var materialTypeObj = convertEnumToObj(materialTypeList);
$.each(materialList, function (index, val) {
    materialObj[val.materialId] = val.nameNorms;
});
layui.use(['laypage', 'table', 'form'], function(){
    var table = layui.table;
    var form = layui.form;

    $(".startTime").datePicker({
        format:"YYYY-MM",
        singleDate:true,  //单选日期
    });
    $(".endTime").datePicker({
        format:"YYYY-MM",
        singleDate:true,  //单选日期
    });
    // $(".update-today").datePicker({
    //     format:"YYYY-MM",
    //     singleDate:true,  //单选日期
    // });

    let todoCols = [
        [
            {field: 'id', title: '标识', width:80, align: 'center', hide: true}
            ,{field: 'orgName', title: '公司', width: 90, align: 'center',templet:function (val) {
                return orgObj[val.orgId];
            }}
            ,{field: 'storeId', title: 'ERP仓位', width: 120, align: 'center', align: 'center',templet:function (val) {
                var storage = storageObj[val.storeId];
                if (!isNull(storage))
                    return storage;
                return '';
            }}
            ,{field: 'materialId', title: '材料名称', width:140, align: 'center',templet:function (val) {
                var material = materialObj[val.materialId];
                if (!isNull(material))
                    return material;
                return '';
            }}
            ,{field: 'controlStoreId', title: '工控仓位', width: 120, align: 'center',templet:function (val) {
                var controlStore = controlStorageObj[val.controlStoreId];
                if (!isNull(controlStore))
                    return controlStore;
                return '';
            }}
            ,{field: 'startDate', title: '盘点开始时间', width:140, align: 'center'}
            ,{field: 'checkDate', title: '盘点结束时间', width:140, align: 'center'}
            ,{field: 'type', title: '盘点类型', width:100, align: 'center',templet:function (val) {
                if (val.type == null || val.type < 0)
                    return '';
                else
                    return inventoryTypeObj[val.type];
            }}
            ,{field: 'initial', title: '期初值(吨)', width:100, align: 'center', templet:function (val) {
                var initial = val.initial;
                if (initial != null)
                    initial = parseFloat(initial / 1000).toFixed(1);
                return initial;
            }}
            ,{field: 'crease', title: '购进值(吨)', width:100, align: 'center', templet:function (val) {
                var crease = val.crease;
                if (crease != null)
                    crease = parseFloat(crease / 1000).toFixed(1);
                return crease;
            }}
            ,{field: 'reduse', title: '消耗值(吨)', width:100, align: 'center', templet:function (val) {
                var reduse = val.reduse;
                if (reduse != null)
                    reduse = parseFloat(reduse / 1000).toFixed(1);
                return reduse;
            }}
            ,{field: 'adjustment', title: '调整值(吨)', width:100, align: 'center', templet:function (val) {
                var adjustment = val.adjustment;
                if (adjustment != null)
                    adjustment = parseFloat(adjustment / 1000).toFixed(1);
                return adjustment;
            }}
            ,{field: 'status', title: '状态', width:80, align: 'center', templet:function (val) {
                if (val.status == 0)
                    return '启用';
                else
                    return '禁用';
            }}
            ,{field: 'creator', title: '创建人', width: 100, align: 'center'}
            ,{field: 'remark', title: '备注', width: 240, align: 'center'}
            // ,{fixed: 'right', title:'操作', width: 150, align:'center', templet:function (val) {
            //     var str = '';
            //     if (val.status == 0)
            //         str = '<a lay-event="delete" href="javascript:void(0)" class="layui-btn layui-btn-xs layui-btn-danger">禁用</a>';
            //     else
            //         str = '<a lay-event="delete" href="javascript:void(0)" class="layui-btn layui-btn-xs layui-btn-normal">启用</a>';
            //     str += '<a lay-event="edit" href="javascript:void(0)" class="layui-btn layui-btn-xs">编辑</a>';
            //     return str;
            // }}
        ]
    ];

    table.render({
        elem: '#body-list'
        ,url: '/material-history/page'
        ,method: 'post'
        ,page: true
        ,cols: todoCols
        ,limit: limit
        ,limits: [30,50,100]
        ,request: {
            pageName: 'curr' //页码的参数名称，默认：page
            ,limitName: 'limit' //每页数据量的参数名，默认：limit
        }
        ,where: {
            offset: 0,
            limit: limit,
            orgId:$('.search-origin').val(),
            // materialId:$('.search-material').val()
        }
        ,parseData: function(res){ //res 即为原始返回的数据
            console.log(res)
            if (res.code == 0 && res.data != null) {
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.records //解析数据列表
                };
            } else {
                return {
                    "code": res.code, //解析接口状态
                    "msg": '没有任何数据', //解析提示文本
                    "count": 0, //解析数据长度
                    "data": '' //解析数据列表
                };
            }
        }
    });

    // form.on('select(search-today)', function(data) {
    //     $(".btn-query").trigger("click");
    // });
    form.on('select(search-storage)', function(data) {
        $(".btn-query").trigger("click");
    });
    form.on('select(search-origin)', function(data) {
        $(".btn-query").trigger("click");
    });
    form.on('select(search-material)', function(data) {
        $(".btn-query").trigger("click");
    });
    $('.btn-query').on('click', function () {//搜索按钮
        var searchVal = $('.search-text').val();
        var orgId = $('.search-origin').val();
        var storageId = $('.search-storage').val();
        var materialId = $('.search-material').val();

        var option = {page:1,limit:limit};
        if (!isNull(searchVal))
            option['searchVal'] = searchVal;
        if (!isNull(materialId))
            option['materialId'] = materialId;
        if (!isNull(storageId))
            option['storageId'] = storageId;
        if (!isNull(orgId))
            option['orgId'] = orgId;
        console.log(option)
        table.reload('body-list', {
            where: option
        });
    });
});